package top.greatxiaozou.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.greatxiaozou.dto.DtoTopic;
import top.greatxiaozou.model.Result;
import top.greatxiaozou.pojo.Topic;
import top.greatxiaozou.service.TopicService;
import top.greatxiaozou.utils.ResultsUtils;
import top.greatxiaozou.utils.TokenUtils;
import top.greatxiaozou.utils.TopicUtils;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/topic")
public class TopicController {

    @Autowired
    TopicService topicService;

    @PostMapping("/save")
    public ResponseEntity<Result> saveTopic(HttpServletRequest request,@RequestBody DtoTopic dtoTopic){
        String token = request.getHeader("token");
        if (TokenUtils.isEffective(token)!=null){
            return TokenUtils.isEffective(token);
        }
   //     System.out.println("过了验证方法");

        String username = (String)TokenUtils.getInfoFromToken(token).get(0);

        Topic topic = TopicUtils.getTopic(dtoTopic, username);
        if (topicService.addTopic(topic)==1){
            return ResultsUtils.success("起飞","帖子添加成功",HttpStatus.OK);
        }
        return ResultsUtils.faild("服务器错误",HttpStatus.FORBIDDEN);
    }
}
